package com.jsgt.greekfire.ui.wallet

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.fhx.core.utils.DialogUtil
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.model.WalletBean
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.dialog.InputDialogUtil
import com.jsgt.greekfire.ui.dialog.SafetyDialog
import com.jsgt.greekfire.ui.me.backup.BackupActivity
import kotlinx.android.synthetic.main.activity_receive.*
import kotlinx.android.synthetic.main.activity_wallet_manager.*
import kotlinx.android.synthetic.main.activity_wallet_manager.rlInfo
import kotlinx.android.synthetic.main.activity_wallet_manager.txvName
import org.jetbrains.anko.find

class WalletManagerActivity : BaseActivity(), View.OnClickListener {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }

    private val safetyDialog: SafetyDialog by lazy {
        SafetyDialog().okListener {
            goToActivity<WalletIdentityManagerActivity> {
                this.putExtra("wallet", wallet)
            }
        }
    }

    private var wallet: IdentityWallet? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_wallet_manager
    }

    @SuppressLint("SetTextI18n")
    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)

        title = getString(R.string.manage)

//        walletViewModel.identityLiveData?.observe(this, Observer {
//            if (it != null) {
//                txvName.text = it.identityName
//
//                walletViewModel.getWallet("ATR")?.observe(this, Observer { it1 ->
//                    if (it1 != null) {
//                        walletBean = it1
//                        txvWalletName.text = it1.name
//                        if (it.backup) {
//                            txvWalletState.visibility = View.INVISIBLE
//                            txvWalletAddress.text = "0x${it1.address}"
//                        } else {
//                            txvWalletState.visibility = View.VISIBLE
//                            txvWalletAddress.text = "0x${hideStr(it1.address!!)}"
//                        }
//                    }
//                })
//            }
//        })
        wallet = intent.getParcelableExtra("wallet")
        if (wallet == null) {
            finish()
        }

        walletViewModel.getIdentityWalletById(wallet?.id!!)?.observe(this, Observer {
            wallet = it
            txvName.text = it.identityName
            txvWalletName.text = it.walletName
            if (it.isBackup) {
                txvWalletState.visibility = View.INVISIBLE
                txvWalletAddress.text = it.address
            } else {
                txvWalletState.visibility = View.VISIBLE
                txvWalletAddress.text = hideStr(it.address!!)
            }
        })

        rlInfo.setOnClickListener(this)
        rlExport.setOnClickListener(this)
        txvWalletAddress.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlInfo -> {
                goToActivity<WalletIdentityManagerActivity>{
                    this.putExtra("wallet", wallet)
                }
            }
            R.id.rlExport -> {
                InputDialogUtil.showInputDialog(
                    this,
                    getString(R.string.modify_wallet_name),
                    getString(R.string.modify_wallet_name_hint)
                ) {
                    if (wallet != null && it.isNotEmpty()) {
                        val name = WalletBean.Name(wallet!!.id.toString(), it)
                        walletViewModel.updateWalletName(name)
                    }
                }
            }
            R.id.txvWalletAddress -> {
                if (wallet != null) {
                    if (wallet!!.isBackup){
                        val clipboardManager = App.context.getSystemService(Context.CLIPBOARD_SERVICE)
                                as android.text.ClipboardManager
                        clipboardManager.text = txvAddress?.text.toString().trim()
                        ToastUtils.show(resources.getString(R.string.copied))
                    }else{
                        safetyDialog.show(supportFragmentManager,"safety")
                    }
                }
            }
        }
    }

    private fun hideStr(str: String): String {
        val length = str.length / 3
        return str.substring(0, length) + "..." + str.substring(str.length - length, str.length)
    }
}